Skip to main content

Tool Execution

BindAI tools are Python functions wrapped by the @tool decorator. When an agent decides to use a tool, BindAI looks up the registered tool and executes the underlying Python function with the arguments supplied to it. The current tool execution model is intentionally simple and predictable.

How Tool Execution Works

The basic execution flow is:
The tool registry identifies the requested tool, while the tool execution layer invokes the underlying Python function.

Creating a Tool

Create a tool by decorating a Python function with @tool.
The decorator converts the function into a BindAI Tool. The resulting tool contains the information BindAI needs to expose and execute the function.

Tool Objects

A Tool represents a callable capability. Conceptually, a tool contains:
The name identifies the tool. The description explains its purpose. The function performs the actual operation. Applications normally create tools through the @tool decorator rather than constructing Tool objects manually.

Tool Names

By default, the tool name comes from the Python function name.
The resulting tool name is:
An explicit name can be supplied when the model-facing name should differ from the Python function name:
The registered tool is then named:
Explicit names can be useful when an application needs a stable or more concise tool name.

Tool Descriptions

A tool description communicates what the operation does. For example:
The description can also be provided explicitly:
Clear descriptions help the model understand when a tool is appropriate. Avoid vague descriptions such as:
Prefer descriptions that identify the operation and its purpose.

Registering a Tool

Tools can be registered directly on an agent.
Tools can also be registered during agent construction.
Multiple tools can be registered together:

The Tool Registry

Registered tools are stored in a ToolRegistry. The registry is responsible for finding a tool by name. Conceptually:
When a tool name is requested, the registry identifies the corresponding Tool. This allows the execution layer to work with tools by their registered names rather than depending directly on a particular Python function.

Tool Executor

BindAI uses a ToolExecutor to execute registered tools. The executor works with a ToolRegistry and resolves the requested tool before invoking it. Conceptually:
The execution layer is responsible for coordinating the lookup and invocation of the registered tool.

What Happens During Execution?

The execution process can be represented as:
A missing tool should not result in an arbitrary Python function being executed. The registry provides the boundary between a requested tool name and an actual registered callable.

Tool Arguments

Arguments are passed to the underlying Python function according to its signature. For example:
The underlying function can receive positional arguments:
or keyword arguments:
The function signature defines which arguments are expected. Keep tool signatures explicit and easy to understand.

Tool Results

Tool execution produces a ToolResult. A successful result conceptually contains:
A failed result can contain an error instead:
The result provides a consistent representation of the outcome of tool execution. Applications should distinguish successful tool output from tool execution failures.

Unknown Tools

If execution receives a tool name that is not registered, the execution layer should not attempt to call an unrelated function. The result represents the failed tool lookup. Conceptually:
This protects the execution path from invalid tool names.

Tool Errors

The executor ultimately delegates the operation to the registered tool. Tools can handle expected application-level failures themselves. For example:
For external services, database operations, and other integrations, handle expected operational failures appropriately. Keep error information useful without exposing unnecessary internal details.

Exceptions

Not every exception should be converted into a normal tool result. Unexpected programming errors should generally remain visible during development so they can be diagnosed and fixed. Expected operational failures may be handled explicitly. For example:
The appropriate strategy depends on the application. Do not silently hide programming errors that should be fixed in the tool implementation.

Tool Context

The current BindAI tool API does not require an implicit ExecutionContext or context.variables object for standard tool execution. Instead, tools should receive the information they need through explicit function parameters. For example:
This makes the tool contract explicit. It also makes the function easier to test independently from an agent execution.

Passing Application State

If a tool needs application-specific state, pass the required information explicitly when that state is part of the tool’s public input. For example:
The tool now clearly communicates which values it needs. For more advanced dependency-management requirements, use normal Python application patterns or the specific API provided by the relevant BindAI component. Do not rely on undocumented global state or implicit context variables.

Avoid Global State

Avoid storing request-specific information in global variables. Avoid:
Prefer explicit parameters:
Explicit inputs make tools easier to:
  • Test
  • Reuse
  • Debug
  • Reason about
  • Execute safely

Tool Execution and Workflows

Tools can be used as part of larger application workflows. A workflow can provide the values required by a tool and use the resulting tool output in later processing. The important distinction is:
  • Tool parameters represent inputs required by the function.
  • Tool output / ToolResult represents the result of execution.
  • Workflow state belongs to the workflow implementation.
  • Agent execution state should not be assumed to be an implicit tool context.
This separation keeps the boundaries between tools and workflows clear.

Tool Lifecycle

A complete tool lifecycle is:
When the tool is used as part of an agent request, the model can receive the resulting tool output and continue the agent execution.

Tool Execution and the Agent

The agent coordinates model interaction and tool execution. Conceptually:
The model decides when a registered capability is useful. BindAI then executes the selected tool and makes its result available to the ongoing agent execution.

Tool Execution and Providers

Tool execution is separated from the specific language model provider. Conceptually:
This separation allows the same application-level tool implementations to be used with different supported providers. The provider handles the model-facing representation of tool calls while BindAI handles application-level tool execution.

Tool Context vs Explicit Inputs

Prefer explicit inputs when the value is genuinely part of the tool’s operation. For example:
This is easier to understand than relying on hidden state such as:
Explicit parameters also make unit tests straightforward.

Tool Context and Security

Explicit inputs do not remove the need for authorization. For example:
The fact that document_id is supplied explicitly does not mean the requested operation should automatically be allowed. The application should still verify:
  • Authentication
  • Authorization
  • Resource ownership
  • Input validity
  • Operation scope
The model should never be treated as a security boundary.

Tool Execution with External Services

When a tool communicates with an external service, keep the external-service logic isolated where practical. For example:
The tool can expose a focused operation while a dedicated client or BindAI connection handles communication details. This approach makes the tool easier to test and keeps external-service concerns separate from agent behavior.

Tool Execution with Memory

Tools can operate alongside BindAI memory. For example:
Memory provides contextual information to the agent. The tool performs an operation. These are separate capabilities and should not be confused with one another. If a tool needs information from application state, pass that information through an explicit interface rather than assuming that memory automatically becomes tool context.

Tool Execution with Knowledge

Tools can also operate alongside BindAI knowledge and retrieval. For example:
Knowledge and retrieval are primarily concerned with finding relevant information. Tools are concerned with executing operations. An application can use both mechanisms in the same agent.

Tool Execution with Multi-Agent Systems

Different agents can have different tool sets. For example:
This allows specialist agents to expose only the capabilities relevant to their roles. Focused tool sets can make multi-agent systems easier to reason about and safer to operate.

Testing Tool Execution

Tool execution should be tested independently from the language model whenever possible. For example, test the underlying operation directly:
Then test the BindAI integration separately. Useful test areas include:
  • Tool creation
  • Tool registration
  • Tool lookup
  • Positional arguments
  • Keyword arguments
  • Successful execution
  • Unknown tools
  • Tool failures
  • Multiple registered tools
  • Security and authorization boundaries
This separation makes failures easier to diagnose.

Complete Example

The tool is registered with the agent and becomes available during agent execution. The model can request the tool when appropriate, BindAI executes the underlying Python function, and the resulting tool output is returned to the ongoing execution.

Best Practices

  • Use explicit function parameters for tool inputs.
  • Add Python type hints.
  • Give tools clear names.
  • Write concise descriptions.
  • Use docstrings when appropriate.
  • Keep tools focused on one responsibility.
  • Avoid request-specific global state.
  • Handle expected application failures clearly.
  • Keep tool outputs predictable.
  • Validate inputs before performing external operations.
  • Keep authorization in application code.
  • Do not expose credentials through tool parameters or results.
  • Do not depend on undocumented execution-context APIs.
  • Test tool functions independently from language-model execution.
  • Use dedicated connections for reusable external integrations.
  • Use workflows for complex orchestration.

Summary

BindAI’s current tool execution model is intentionally straightforward. A Python function is converted into a Tool using @tool, registered with an agent, resolved through the tool registry, and executed as part of agent execution. The underlying Python function receives the arguments defined by its signature, and the execution system represents the outcome through tool-result handling. For standard tools, inputs should be explicit rather than relying on an implicit ExecutionContext or context.variables abstraction. This design keeps tools:
  • Explicit
  • Testable
  • Reusable
  • Provider-independent
  • Easier to secure
Tools therefore provide a clear boundary between model-driven tool selection and trusted application code.